<!--
 * @Author: caozaihui 15908754730@163.com
 * @Date: 2024-06-14 00:47:40
 * @LastEditors: czh 2119294696@qq.com
 * @Description: 登录页面
-->

<script setup>
import LayoutHeader from '../Layout/components/LayoutHeader.vue';
import { useRouter } from 'vue-router';
// 引入图标
import { TopRight, MagicStick } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue';
import { getCaptha, login } from '@/apis/login';
import { randomStr } from '@antfu/utils';
import { useAccountStore } from '@/stores/useAccountStore.js'
import LayoutFooter from '@/components/LayoutFooter.vue';

const router = useRouter();

const backHome = () => {
  router.push({
    path: '/'
  })
}
const register = () => {
  router.push({
    path: '/register'
  })
}

const msg = ref('')
const agree = ref(1)
const value = ref(5)
// 生成随机字符串key并保存在本地
const key = ref(randomStr())

const captcha = ref("")
const refreshCaptcha = () => {
  console.log('获取验证码');
  getCaptha(key).then(res => {
    let url = window.URL.createObjectURL(res);
    captcha.value = url
  })
}

// 登录参数
const loginPara = ref({
  account: '',
  code: '',
  key: '',
  password: ''
})
// 使用用户数据
const accountStore = useAccountStore()

// 点击登录上传key以及参数
const access = () => {
  loginPara.value.key = key.value
  // 获取登录结果
  login(loginPara.value).then(res => {
    console.log(res.code);
    if (res.code != '0') {
      msg.value = res.msg
    } else {
      //存储数据
      const account = res
      //将用户信息存储在cookie中
      accountStore.setAccount(account)
      //登陆成功跳转首页 并传递数据
      router.push({
        path: '/loading',
      })
    }
  })
}


onMounted(async () => {
  getCaptha(key).then(res => {
    let url = window.URL.createObjectURL(res);
    captcha.value = url;
  })
})


</script>

<template>
  <LayoutHeader />
  <!-- 登录组件 -->

  <el-row style="margin-bottom: 30px;">
    <el-col :lg="9" :xs="0"></el-col>
    <el-col :lg="6" :xs="24">
      <!-- 登陆卡片 -->
      <div class="vague">
        <div class="box-card vague_content">
          <div class="card-header">
            <img style="height: 60px;" src="@/assets/imgs/logo.png">
            <el-rate v-model="value" />
          </div>
          <div>
            <!-- 账号 -->
            <el-input class="input" v-model="loginPara.account" placeholder="输入你的账号">
              <template #prepend>
                <el-icon>
                  <TopRight />
                </el-icon>
              </template>
            </el-input>
            <!-- 密码 -->
            <el-input class="input" v-model="loginPara.password" show-password placeholder="你的密码">
              <template #prepend>
                <el-icon>
                  <TopRight />
                </el-icon>
              </template>
            </el-input>
            <!-- 验证码 -->
            <div style="margin-top: 5px;">
              <el-input class="input-mini" v-model="loginPara.code" placeholder="输入验证码">
                <template #append>
                  <el-button @click="refreshCaptcha">刷新</el-button>
                </template>
              </el-input>
              <!-- 验证码图片 -->
              <img class="img" :src=captcha>
            </div>
            <!-- 跳转回首页 -->
            <div>
              <el-button size="small" plain @click="backHome">返回首页</el-button>
              <el-text type="danger" style="margin-left: 15px;">{{ msg }}</el-text>
            </div>

            <!-- 登录按钮/注册 -->
            <div style="text-align:center;margin-top: 50px;">
              <el-button-group>
                <!-- 登录 -->
                <el-button class="button" type="primary" size="large" @click="access">登录</el-button>
                <!-- 注册 -->
                <el-button type="primary" @click="register" size="large">注册
                  <el-icon style="margin-left: 10px;" size="20">
                    <MagicStick />
                  </el-icon>
                </el-button>
              </el-button-group>
            </div>
            <!-- 协议框 -->
            <div style="margin-top: 30px;text-align: center;">
              <el-switch v-model="agree" />
              <el-text class="title">同意FoRum《用户隐私协议》</el-text>
            </div>
          </div>
        </div>
      </div>
    </el-col>
    <el-col :lg="9" :xs="0"></el-col>
  </el-row>

  <LayoutFooter />
</template>

<style scoped>
.vague {
  position: relative;
  margin:40px auto;
  border-radius: 30px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  padding: 20px;
}

.vague_content {
  position: relative;
  color: #fff;
  z-index: 10;
}

.vague::after {
  position: absolute;
  content: "";
  background-image: url("https://images.unsplash.com/photo-1440688807730-73e4e2169fb8?dpr=1&auto=format&fit=crop&w=1500&h=1001&q=80&cs=tinysrgb&crop=");
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  background-position: top;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  filter: blur(10px);
  z-index: 0;
}

.button {
  width: 160px;
}

.input {
  margin-top: 25px;
  height: 38px;
  width: 100%;
}

.input-mini {
  margin-top: 20px;
  height: 38px;
  width: 50%;
}

.title {
  font-size: 13px;
  color: #444444;
  padding-left: 5px;
}

.img {
  height: 50px;
  margin-top: 20px;
  margin-left: 10px;
}

.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  margin-top: 30px;
  margin-left: 0;
  width: 100%;
  height: 480px;
  border-radius: 15px;
}
</style>